{extend name="layout" /}
{block name="style"}
<style>
	.search-title{height: 33px;line-height: 33px;}
</style>
{/block}
{block name="content"}
<div class="row">
	<div class="col-xs-12">
		<div class="box">
			<div class="box-header with-border">
				<h3 class="box-title">{$page_title}</h3>
			</div>
			<div class="box-body">
				<div class="tab-pane active">

					<div class="block-content row">
						<div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12" id="form_group_major">
							<div class="col-xs-1 label-title search-title" for="major">{:lang('主表')}</div>
							<div class="col-xs-2">
								<select class="select2 form-control" id="major" name="table_name">
									<option>{:lang('请选择主表')}</option>
									{volist name="$tableData" id="option"}
									<option value="{$option.table_name}" >{$option.table_comment|raw}</option>
									{/volist}
								</select>
							</div>
							<!--<div class="col-xs-1 label-title search-title" for="relation">{:lang('关联表')}</div>
							<div class="col-xs-4">
								<select class="select2 form-control" id="relation" name="relation">
									{volist name="$tableData" id="option"}
									<option value="{$option.table_name}" >{$option.table_comment|raw}</option>
									{/volist}
								</select>
							</div>-->
						</div>
					</div>

					<div class="block-content row">
						<div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12" id="form_group_table">
							<!--<div class="col-xs-1 label-title search-title" for="table">{:lang('查询表')}</div>
							<div class="col-xs-2">
								<select class="select2 form-control" id="table" name="table">

								</select>
							</div>-->

							<div class="col-xs-1 label-title search-title" for="fileds">{:lang('查询字段')}</div>
							<div class="col-xs-2">
								<select class="select2 form-control" id="fileds" name="fileds">
								</select>
							</div>

							<div class="col-xs-1 label-title search-title" for="select_type">{:lang('查询条件')}</div>
							<div class="col-xs-2">
								<select class="select2 form-control" id="select_type" name="select_type">
									<option value="=">{:lang('等于')}</option>
									<option value=">">{:lang('大于')}</option>
									<option value="<">{:lang('小于')}</option>
									<option value="<=">{:lang('小于等于')}</option>
									<option value=">=">{:lang('大于等于')}</option>
									<option value="like">{:lang('相似')}</option>
								</select>
							</div>

							<div class="col-xs-1 label-title search-title" for="keyword">{:lang('查询关键词')}</div>
							<div class="col-xs-2">
								<input type="text" class="form-control" id="keyword" name="keyword" placeholder="{:lang('请输入关键词')}" value="">
							</div>
						</div>
					</div>

					<div class="block-content row">
						<div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12" id="form_group_table2">
							<div class="col-xs-1 label-title search-title" for="show_fields">{:lang('显示字段')}</div>
							<div class="col-xs-5">
								<select class="select2 form-control" id="show_fields" name="show_fields" multiple="multiple" >
								</select>
							</div>
							<div class="col-xs-2">
								<button class="btn btn-primary btn-flat create-sql" type="botton" autocomplete="on"> {:lang('生成语句')} </button>
								<button class="btn btn-primary btn-flat excute-sql" type="botton" autocomplete="on" style="display: none"> {:lang('执行语句')} </button>
								<!--<button class="btn btn-default btn-flat" type="button" onclick="javascript:history.back(-1);return false;"> {:lang('添加更多条件')} </button>-->
							</div>

						</div>
					</div>



					<div class="block-content row">
						<div class="form-group col-lg-12 col-md-12 col-sm-12 col-xs-12" id="form_group_sql">
							<div class="col-xs-12 label-title" for="sql_query">{:lang('查询')}SQL</div>
							<div class="col-xs-12">
								<textarea class="form-control" id="sql_query" rows="7" name="sql_query" readonly placeholder="{:lang('请输入')}SQL{:lang('语句')}"></textarea>
								<div class="help-block">{:lang('每行一条语句')}，{:lang('每行请用')};{:lang('结尾')}，{:lang('只支持')}select{:lang('查询')}</div>
							</div>
						</div>
						<!--							<div class="form-group col-md-12 col-xs-12">-->
						<!--								<div class="col-xs-12">-->
						<!--									<button class="btn btn-primary btn-flat db-run" type="botton" autocomplete="on"> {:lang('提交')} </button>-->
						<!--									<button class="btn btn-default btn-flat" type="button" onclick="javascript:history.back(-1);return false;"> {:lang('返回')} </button>-->
						<!--								</div>-->
						<!--							</div>-->

					</div>
					<div class="block-content" style="padding:0 15px">
						<pre id="result" style="display: none;">
							<table class="table table-builder table-hover table-bordered table-striped">
									<thead>
										<tr>
											<th>{:lang('名称')}</th>
											<th>{:lang('图标')}</th>
											<th>{:lang('版本')}</th>
											<th>{:lang('模块标识')}</th>
											<th>{:lang('作者')}</th>
											<th>{:lang('简介')}</th>
										</tr>
									</thead>
									<tbody>
									<tr>
										<td>{:lang('资讯')}</td>
										<td><i class="fa fa-fw"></i></td>
										<td>1.0.0</td>
										<td>cms.zbphp.module</td>
										<td><a href="javascript:;" target="_blank">ericYan</a></td>
										<td>{:lang('文章资讯管理')}</td>
										<td class="text-center"><a class="btn btn-xs btn-noborder btn-default btn-flat ajax-get confirm" href="/admin.php/admin/module/update/name/cms.html">{:lang('更新')}</a> <a class="btn btn-xs btn-noborder btn-default btn-flat ajax-get confirm" href="/admin.php/admin/module/disable/ids/7.html">{:lang('禁用')}</a> <a class="btn btn-xs btn-noborder btn-default btn-flat" href="/admin.php/admin/module/export/name/cms.html">{:lang('导出')}</a> <a class="btn btn-xs btn-noborder btn-default btn-flat" href="/admin.php/admin/module/uninstall/name/cms.html">{:lang('卸载')}</a> </td>
									</tr>
									</tbody>

								</table>
						</pre>
					</div>
				</div>
			</div>
		</div>
		<!-- /应用列表 -->
	</div>
</div>
{/block}

{block name="script"}
<script type="text/javascript">
	/*$(".create-sql").click(function(){
        var self = this;
        $.post('createSql',{sql:$('#sql_query').val(), type:1}, success);

        function success(data){
            if(data.code){
                Stars.notify('执行成功', 'success');
            }
            $('#sql_query').html(JSON.stringify(data.data, null, "\t"));
        }
    });*/

	//获取表字段
	$('#major').change(function(){
		var _table_name = $(this).val();
		var url = 'getTableField';
		$('#fields').val('');
		$('#show_fields').val('');
		$.post(url,{table_name:_table_name}, success);
		function success(ret){
			if(ret.code){
				var _option = '';
				$.each(ret.data,function(i,val){
					 _option += "<option value='"+val.Field+"'>"+val.Comment+"</option>";
				});
				$('#fileds').html(_option);
				$('#show_fields').html(_option);
			}
		}
	})
	//生成sql语句
	$('.create-sql').click(function(){
		var _table_name = $('#major').val();
		var _fileds = $('#fileds').val();
		var _select_type = $('#select_type').val();
		var _keyword = $('#keyword').val();
		var _show_fields = $('#show_fields').val();
		console.log(_show_fields);
		var url = 'createSql';
		// if(!_show_fields){
		// 	alert('请选择显示字段');
		// 	return false;
		// }
		var param = {
			table_name:_table_name,
			fileds:_fileds,
			select_type:_select_type,
			keyword:_keyword,
			show_fields:_show_fields,
		};
		$.post(url,param, success);
		function success(ret){
			console.log(JSON.stringify(ret));
			if(ret.code){
				$('#sql_query').text(ret.data);
				$('.excute-sql').show();
			}
		}
	})
	//执行sql语句
	$('.excute-sql').click(function(){
		var _sql = $('#sql_query').val();
		var _show_fields = $('#show_fields').val();
		var _table = $('#major').val();
		//_show_fields = ['shortname','merger_name','pinyin'];
		var url = 'excuteSql';
		var param = {
			sql:_sql,
			table:_table
		};
		console.log(_show_fields);
		$.post(url,param, success);
		function success(ret){
			console.log(JSON.stringify(ret));
			if(ret.code){
				$('#result table').html('');
				$('#result').show();
				var _th = _tr = '';
				console.log('_show_fields:' + _show_fields);
				if(_show_fields == '' || _show_fields == null){
					$.each(ret.data.fields,function(i,val){
						_th += "<th>"+val.Comment+"</th>";
					});
				}else{
					$.each(_show_fields,function(i,val){
						_th += "<th>"+val+"</th>";
					});
				}
				console.log('_show_fields:' + _th);
				var _thead = "<thead>" +
						"<tr>" +_th+"</tr>" +
						"</thead>";

				$.each(ret.data.data,function(i,val){

					_td = '';
					$.each(val,function(ii,vv){
						console.log(ii);
						_td += "<td>"+val[ii]+"</td>";
					})
					_tr += "<tr>"+_td+"</tr>";
				});
				var _tbody = "<tbody>"+_tr+"</tbody>";
				$('#result table').html(_thead+_tbody);
			}
		}
	})
</script>
{/block}